//第4关 综合应用

package step4;
import java.util.ArrayList;
import java.util.List;

public class StudentDAOImpl implements IStudentDAO {

	static List<Student> students = new ArrayList<Student>();

	@Override
	public void insertStudent(Student stu) {
		students.add(stu);
	}

	@Override
	public void deleteStudent(String sno) {
		for(Student stu:students) {
			if(sno.equals(stu.getSno())) {
				students.remove(stu);
				break;
			}
		}

	}

	@Override
	public void updateStudent(Student stu) {
		for(Student s:students) {
			if(stu.getSno().equals(s.getSno())) {
				s.setSname(stu.getSname());
				s.setSdept(stu.getSdept());
			}
		}

	}

	@Override
	public Student findStudentBySno(String sno) {
		for (Student stu:students) {
			if(sno.equals(stu.getSno())) {
				return stu;
			}
		}
		return null;
	}

	@Override
	public void displayStudentBySno(String sno) {
		for (Student stu:students) {
			if(sno.equals(stu.getSno())) {
				System.out.println(stu);
			}
		}

	}

	@Override
	public void displayAllStudent() {
		if (students.size() > 0) {
			for (Student stu : students) {
				System.out.println(stu);
			}
		} else {
			System.out.println("数据库中无学生记录!");
		}
	}

}
